/**
 * 
 */
package com.cipe.cmrs.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cipe.cmrs.model.State;
import com.cipe.cmrs.service.CountryService;
import com.cipe.cmrs.service.StateService;

/**
 * @author Incepio
 *
 */
@Controller
public class StateController extends BaseController {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= -3823562098119817733L;
	@Autowired
	private StateService		stateService;
	@Autowired
	private CountryService		countryService;

	@RequestMapping("/secure/saveState.do")
	public String SaveState(@ModelAttribute("state") State state, BindingResult result, Model model, HttpServletRequest request) {
		try {
			state = stateService.saveState(state, getUserId(request));
			saveMessage(request, getText("editState.message.success"));
		} catch (Exception e) {
			logger.error(getText("error.transaction"), e);
			saveError(request, getText("error.transaction"));
		}
		model.addAttribute("state", state);
		return "editState";
	}

	@RequestMapping("/secure/showAddState.do")
	public String showAddState(Model model) {
		model.addAttribute("state", new State());
		model.addAttribute("countryList", countryService.findAll());
		return "editState";
	}

	@RequestMapping("/secure/showEditState.do")
	public String showEditState(@RequestParam Integer id, Model model) {
		model.addAttribute("state", stateService.findById(id));
		model.addAttribute("countryList", countryService.findAll());
		return "editState";
	}

	@RequestMapping("/secure/showStateList.do")
	public String showStateList(Model model) {
		model.addAttribute("states", stateService.findAll());
		return "stateList";
	}
}
